package com.example.wdt.wdt;

import cn.wangdian.erp.sdk.api.aftersales.RawRefundAPI;
import cn.wangdian.erp.sdk.api.aftersales.RefundAPI;
import cn.wangdian.erp.sdk.api.finance.FinanceAPI;
import cn.wangdian.erp.sdk.api.finance.PlatformBillAPI;
import cn.wangdian.erp.sdk.api.finance.SettleAPI;
import cn.wangdian.erp.sdk.api.finance.TransferAPI;
import cn.wangdian.erp.sdk.api.goods.*;
import cn.wangdian.erp.sdk.api.process.ProcessAPI;
import cn.wangdian.erp.sdk.api.purchase.PurchaseProviderGoodsAPI;
import cn.wangdian.erp.sdk.api.purchaseOrder.PurchaseOrderAPI;
import cn.wangdian.erp.sdk.api.purchaseapply.PurchaseApplyAPI;
import cn.wangdian.erp.sdk.api.purchasereturn.PurchaseReturnAPI;
import cn.wangdian.erp.sdk.api.sales.LogisticsSyncAPI;
import cn.wangdian.erp.sdk.api.sales.RawTradeAPI;
import cn.wangdian.erp.sdk.api.sales.StockOutAPI;
import cn.wangdian.erp.sdk.api.sales.StockSyncAPI;
import cn.wangdian.erp.sdk.api.setting.SettingAPI;
import cn.wangdian.erp.sdk.api.statistic.StockAccountApi;
import cn.wangdian.erp.sdk.api.statistic.TradeAnalysisAPI;
import cn.wangdian.erp.sdk.api.wms.StockAPI;
import cn.wangdian.erp.sdk.api.wms.moveorder.MoveOrderAPI;
import cn.wangdian.erp.sdk.api.wms.outer.in.OuterInAPI;
import cn.wangdian.erp.sdk.api.wms.outer.out.OuterOutAPI;
import cn.wangdian.erp.sdk.api.wms.position.PositionAPI;
import cn.wangdian.erp.sdk.api.wms.stockdefect.DefectChangeAPI;
import cn.wangdian.erp.sdk.api.wms.stockin.StockinAPI;
import cn.wangdian.erp.sdk.api.wms.stockother.in.StockOtherInAPI;
import cn.wangdian.erp.sdk.api.wms.stockother.out.StockOtherOutAPI;
import cn.wangdian.erp.sdk.api.wms.stockout.StockoutAPI2;
import cn.wangdian.erp.sdk.api.wms.stockpd.StockPdAPI;
import cn.wangdian.erp.sdk.api.wms.stockspec.StockSpecAPI;
import cn.wangdian.erp.sdk.impl.Api;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class WtdApiReflector {

    // 存储接口名、方法名、注解value和方法信息的映射
    private static Map<String, Method> apiMap = new HashMap<>();

    static {
        reflectInterfaces(RawRefundAPI.class);
        reflectInterfaces(RefundAPI.class);
        reflectInterfaces(FinanceAPI.class);
        reflectInterfaces(PlatformBillAPI.class);
        reflectInterfaces(SettleAPI.class);
        reflectInterfaces(TransferAPI.class);
        reflectInterfaces(GoodsAPI.class);
        reflectInterfaces(GoodsBrandApi.class);
        reflectInterfaces(GoodsCategoryAPI.class);
        reflectInterfaces(GoodsClassAPI.class);
        reflectInterfaces(GoodsSuiteAPI.class);
        reflectInterfaces(ProcessAPI.class);
        reflectInterfaces(PurchaseProviderGoodsAPI.class);
        reflectInterfaces(PurchaseApplyAPI.class);
        reflectInterfaces(PurchaseOrderAPI.class);
        reflectInterfaces(PurchaseReturnAPI.class);
        reflectInterfaces(LogisticsSyncAPI.class);
        reflectInterfaces(RawTradeAPI.class);
        reflectInterfaces(StockOutAPI.class);
        reflectInterfaces(StockSyncAPI.class);
        reflectInterfaces(SettingAPI.class);
        reflectInterfaces(StockAccountApi.class);
        reflectInterfaces(TradeAnalysisAPI.class);
        reflectInterfaces(MoveOrderAPI.class);
        reflectInterfaces(OuterInAPI.class);
        reflectInterfaces(OuterOutAPI.class);
        reflectInterfaces(PositionAPI.class);
        reflectInterfaces(DefectChangeAPI.class);
        reflectInterfaces(StockinAPI.class);
        reflectInterfaces(StockOtherInAPI.class);
        reflectInterfaces(StockOtherOutAPI.class);
        reflectInterfaces(StockoutAPI2.class);
        reflectInterfaces(StockPdAPI.class);
        reflectInterfaces(StockSpecAPI.class);
        reflectInterfaces(StockAPI.class);

    }

    // 反射接口并存储方法信息
    private static void reflectInterfaces(Class<?> apiInterface) {
        if (!apiInterface.isInterface()) {
            throw new IllegalArgumentException("The provided class is not an interface.");
        }

        for (Method method : apiInterface.getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !method.isBridge() && !method.isSynthetic()) {
                Api apiAnnotation = method.getAnnotation(Api.class);
                if (apiAnnotation != null) {
                    String apiValue = apiAnnotation.value();
                    apiMap.put(apiValue, method);
                }
            }
        }

    }

    // 根据接口名和注解value查找方法信息
    public static Method getMethod(String apiValue) {
        Method method = apiMap.get(apiValue);
        if (method != null) {
            return method;
        }
        return null;
    }


}
